package com.hl.card.modular.packBack.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.constant.factory.PageFactory;
import com.hl.card.common.persistence.model.SysDicType;
import com.hl.card.common.persistence.model.SysDicTypeModel;
import com.hl.card.modular.packBack.model.Config;
import com.hl.card.modular.packBack.model.ConfigAction;
import com.hl.card.modular.packBack.model.ConfigStyle;
import com.hl.card.modular.packBack.model.ConfigType;
import com.hl.card.modular.packBack.service.ConfigService;


@Controller
@RequestMapping("/config")
public class ConfigController{
	@Autowired
	private ConfigService<Config> configService;
	
	private String PREFIX = "/packback/config/";
	
    @RequestMapping("")
    public String index(){
        return PREFIX+"config.html";
    }
    /**
     * 跳转到添加配置
     */
    @RequestMapping("/add")
    public String configAdd(Model model) {
    	List<ConfigType> list = configService.selectConfigTypeList();
        List<ConfigStyle> styleList=configService.selectConfigStyleList();
        List<ConfigAction> actionList=configService.selectConfigActionList();
        List<SysDicTypeModel> dicList=configService.getDicList();
        model.addAttribute("configTypeList",list);
        model.addAttribute("configActionList",actionList);
        model.addAttribute("configStyleList",styleList);
        model.addAttribute("dicList",dicList);
        return PREFIX + "/config_add.html";
    }
	/**
	 * 新增配置
	 * @param config
	 */
	@RequestMapping("/addConfig")
	@ResponseBody
	public Object addConfig(@RequestBody Config config)
	{
		try {
			configService.addConfig(
			()->{
				return config;
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		Map<String,Object> result=new HashMap<String, Object>();
		result.put("code", 200);
		result.put("message", "操作成功！");
		return result;
	}
	
	/**
	 * 更新配置
	 * @param config
	 */
	@RequestMapping("/updateConfig")
	@ResponseBody
	public Object updateConfig(@RequestBody Config config)
	{
		try {
			configService.updateConfig(
					()->{
					return config;
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
		Map<String,Object> result=new HashMap<String, Object>();
		result.put("code", 200);
		result.put("message", "操作成功！");
		return result;
	}
	/**
	 * 删除配置
	 * @param config
	 */
	@RequestMapping("/removeConfig")
	@ResponseBody
	public Object removeConfig(Config  config)
	{
		configService.removeConfig(
				()->{
				return config;
		});
		Map<String,Object> result=new HashMap<String, Object>();
		result.put("code", 200);
		result.put("message", "操作成功！");
		return result;
	}
	
    
	/**
	 * 查询配置
	 * @param config
	 */
	@RequestMapping("/getConfig")
	public void getConfig(Config config)
	{
		configService.searchConfig(
				()->{
				return config;
		});
	}
	
	/**
	 * 获取单个配置值
	 * @param config 
	 */
	@RequestMapping("/getConfigByCode/{configCode}")
	public Object getConfigByCode(@PathVariable String configCode, Model model)
	{
		Config c=configService.getConfigByCode(configCode);
		List<ConfigType> list = configService.selectConfigTypeList();
		List<ConfigAction> actionList=configService.selectConfigActionList();
		list.add(new ConfigType(c.getConfigType()));
        List<ConfigStyle> styleList=configService.selectConfigStyleList();
        List<SysDicTypeModel> dicList=configService.getDicList();
        model.addAttribute("configTypeList",list);
        model.addAttribute("configActionList",actionList);
        model.addAttribute("configStyleList",styleList);
		model.addAttribute("config",c);
		model.addAttribute("dicList",dicList);
		return PREFIX+"config_edit.html";
	}
	/**
	 * 查询配置列表
	 * @param config
	 */
	@RequestMapping("/configList")
	@ResponseBody
	public Object configList(String configName)
	{
		Page<Config> page = new PageFactory<Config>().defaultPage();
		List<Config> configList=configService.configList(page,configName);
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("rows", configList);
		result.put("total", page.getTotal());
		return result;
	}
}
